import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtWidgets import QMessageBox,  QLabel
from PyQt5.QtCore import QTimer, QThread, Qt
from Ui_mainwin import Ui_MainWindow
from PyQt5.QtGui import QPixmap


class DWTRWindow(Ui_MainWindow, QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowTitle("喝水提醒器")
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.show()
        self.pushButton.clicked.connect(self.linkStart)
        self.verticalSlider.valueChanged.connect(
            self.handle_slider_value_changed)
        self.timer_min_value = 10

        # 设置定时器
        self.start_timer = QTimer()
        self.start_timer.timeout.connect(self.showPopup)
        self.close_timer = QTimer()
        self.close_timer.timeout.connect(self.closePopup)
        self.qmsg_box = QMessageBox()
        pixmap = QPixmap("drinkingwater.jpg")
        label = QLabel()
        label.setPixmap(pixmap.scaledToWidth(400))
        self.qmsg_box.setWindowTitle("喝水!!!")
        self.qmsg_box.layout().addWidget(label)
        self.qmsg_box.closeEvent = self.onMessageBoxClose
        self.qmsg_box.accepted.connect(self.onMessageBoxAccepted)

    def handle_slider_value_changed(self, value):
        self.timer_min_value = 10*(value+1)
        self.label_2.setText(f"{self.timer_min_value}min")

    def linkStart(self):
        print("start timer start.")
        self.start_timer.stop()
        self.close_timer.stop()
        self.start_timer.start(self.timer_min_value*60*1000)

    def showPopup(self):
        print("start timer out.")
        self.start_timer.stop()
        print("close timer start.")
        self.close_timer.start(10000)
        self.qmsg_box.exec_()

    def closePopup(self):
        print("close timer out.")
        self.close_timer.stop()
        self.qmsg_box.accept()

    def onMessageBoxClose(self, event):
        print("Manul closed.")
        self.close_timer.stop()
        self.linkStart()

    def onMessageBoxAccepted(self):
        print("MessageBox accepted.")
        self.linkStart()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_win = DWTRWindow()
    sys.exit(app.exec_())
